package com.lh.java.basic.threadlocal;

public class Thread9 {

	/**
	 * 功能描述  
	 * @Author: lh  2013-3-29 下午12:14:01
	 */
	public static void main(String[] args) {
		Object9 b9 = new Object9();
		Thread t1 = new Thread(new R1(b9),"t1");
		Thread t2 = new Thread(new R2(b9),"t2");
		t1.start();
		t2.start();

	}

}
class Object9{
    public int a=5;
    public synchronized void add() {
    	try {
			Thread.sleep(60);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		a+=1;
		System.out.println(Thread.currentThread().getName());
		System.out.println("add "+a);
	}
    public synchronized void dec() {
    	try {
			Thread.sleep(60);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		a+=1;
		System.out.println(Thread.currentThread().getName());
		a-=1;
		System.out.println("dec "+a);
	}
}
class R1 implements Runnable{
	private Object9 b9;
     public R1(Object9 b9){
    	 this.b9= b9;
     }
	@Override
	public void run() {
		for (int i = 0; i < 9; i++) {
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			b9.add();
		}
				
	}
	
}

class R2 implements Runnable{
	private Object9 b9;
     public R2(Object9 b9){
    	 this.b9= b9;
     }
	@Override
	public void run() {
		for (int i = 0; i < 9; i++) {
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			b9.dec();
		}
				
	}
	
}
